package com.example.administrator.smartbutler.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.smartbutler.MainActivity;
import com.example.administrator.smartbutler.R;
import com.example.administrator.smartbutler.entity.MyUser;
import com.example.administrator.smartbutler.utils.ErroeCodeUtils;
import com.example.administrator.smartbutler.utils.ShareUtils;
import com.example.administrator.smartbutler.view.CustomDialog;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 项目名:SmartButler
 * 包名:com.example.administrator.smartbutler.ui
 * 文件名：LoginActivity
 * 创建者：justin
 * Created by Administrator on 2018/1/19.
 * 描述：TODO
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button register;

    private Button login;

    private EditText login_edt_username;

    private EditText login_edt_pwd;

    private CheckBox login_chk_rememberPwd;

    private TextView forgetPassword;

    private CustomDialog customDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView() {
        register = (Button) findViewById(R.id.login_btn_register);

        login = (Button) findViewById(R.id.login_btn_login);

        login_edt_username = (EditText) findViewById(R.id.login_edt_username);

        login_edt_pwd = (EditText) findViewById(R.id.login_edt_pwd);

        login_chk_rememberPwd = (CheckBox) findViewById(R.id.login_chk_rememberPwd);

        forgetPassword = (TextView) findViewById(R.id.forgetPassword);


        boolean isCheck = ShareUtils.getBoolean(this, "rememberPwd", false);

        login_chk_rememberPwd.setChecked(isCheck);

        if (isCheck) {
            login_edt_username.setText(ShareUtils.getString(this, "name", ""));
            login_edt_pwd.setText(ShareUtils.getString(this, "password", ""));
        }


        register.setOnClickListener(this);

        login.setOnClickListener(this);

        forgetPassword.setOnClickListener(this);


        //设置dialog
        customDialog = new CustomDialog(this, 100, 100, R.layout.dialog_loding, R.style.theme_dialog, Gravity.CENTER, R.style.pop_anim_style);

        //屏幕外点击无效
        customDialog.setCancelable(false);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn_register:
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            case R.id.login_btn_login:

                //获取输入框的值
                String name = login_edt_username.getText().toString().trim();
                String pwd = login_edt_pwd.getText().toString().trim();

                //判断是否为空
                if (!TextUtils.isEmpty(name) & !TextUtils.isEmpty(pwd)) {
                    customDialog.show();

                    MyUser user = new MyUser();

                    user.setUsername(name);
                    user.setPassword(pwd);

                    user.login(new SaveListener<MyUser>() {
                        @Override
                        public void done(MyUser myUser, BmobException e) {
                            customDialog.dismiss();

                            if (e == null) {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();

                            } else {
                                Toast.makeText(LoginActivity.this, "登录失败," + ErroeCodeUtils.findError(e.getErrorCode()), Toast.LENGTH_SHORT).show();
                            }

                        }
                    });


                } else {
                    Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.forgetPassword:

                startActivity(new Intent(LoginActivity.this, forgetPasswordActivity.class));

                break;
        }

    }

    //假如我现在输入用户名和密码，但是我不点击登录，而是直接退出
    @Override
    protected void onDestroy() {
        super.onDestroy();

        //保存状态

        ShareUtils.putBoolean(this, "rememberPwd", login_chk_rememberPwd.isChecked());

        if (login_chk_rememberPwd.isChecked() == true) {

            //记住密码和用户名
            ShareUtils.putString(this, "name", login_edt_username.getText().toString().trim());
            ShareUtils.putString(this, "password", login_edt_pwd.getText().toString().trim());

        } else {
            //清空密码和用户名
            ShareUtils.deleShare(this, "name");
            ShareUtils.deleShare(this, "password");
        }

    }
}
